from py_stealth import *
import time

materials = [0x1012]
pet = 0x00020477
lastFeedTime = time.time()
meats = [0x09C0, 0x09B7, 0x09F2, 0x09C9]
fruits = [0x097D, 0x09D2, 0x097B, 0x09EB, 0x09D1, 0x09D0]

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y


def CheckLocation(originLocation):
    currentLocation = Point(GetX(Self()), GetY(Self()))
    shiftLocation = abs(originLocation.x - currentLocation.x) + abs(originLocation.y - currentLocation.y)
    if shiftLocation > 40:
        NewMoveXY(originLocation.x, originLocation.y, False, 200, True)


def FeedPet():
    global lastFeedTime
    current_time = time.time()
    if abs(lastFeedTime - current_time) > 60*60:
        CreateFood()
        AddToJournal('Find meats start')
        FindTypeArrayEx(meats, [0x0000], [Backpack()], False)
        AddToJournal('Find meats end')
        CheckLag(1000)
        Wait(1000)
        MoveItem(FindItem(), 1, pet, 0, 0, 0)
        lastFeedTime = current_time
        AddToJournal('feed pet end')

def CreateFood():
    while FindTypeArrayEx(meats, [0x0000], [Backpack()], False) == 0:
        Cast('Create Food')
        CheckLag(1000)
        Wait(1000)
    for trash in fruits:
        while FindType(trash, Backpack()) > 0:
            Drop(FindItem(), FindQuantity(), GetX(Self())+1, GetY(Self())-1, GetZ(Self()))
            CheckLag(1000)
            Wait(1000)

def LoopCorpse():
    SetFindDistance(22)
    while FindType(0x2006, Ground()) > 0:
        corpses = GetFindedList()
        for corpse in corpses:
            AddToJournal('start loop: '+GetTooltip(corpse))
            NewMoveXY(GetX(corpse), GetY(corpse), False, 1, True)
            UseObject(corpse)
            Wait(600)
            for material_type in materials:
                if FindType(material_type, corpse) > 0:
                    Grab(FindItem(), FindQuantity())
                    CheckLag(1000)
                    Wait(600)
            Ignore(corpse)
            AddToJournal('loop end')


def Main():
    originPoint = Point(GetX(Self()), GetY(Self()))
    while not Dead():
        FeedPet()
        LoopCorpse()
        if TargetPresent():
            CancelTarget()
        if GetHP(Self()) < GetMaxHP(Self()):
            CastToObj('Heal', Self())
            CheckLag(1000)
            Wait(600)
        elif GetHP(pet) < GetMaxHP(pet) * 0.8:
            if IsPoisoned(pet):
                CastToObj('Cure', pet)
            else:
                CastToObj('Heal', pet)
            CheckLag(1000)
            Wait(600)
        elif not Hidden():
            CastToObj('Invisibility', Self())
            CheckLag(3000)
            Wait(1000)
        else:
            NewMoveXY(originPoint.x, originPoint.y, False, 0, True)
Main()
